<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="metronic/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
    <link href="metronic/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css"/>
    <link href="metronic/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
    <link href="metronic/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
</head>
<body class="login">
<div class="content">
    <h3 class="form-title">商品详情</h3>
    <div id="seckillmsg" class="form-group">
        <label style="color:red" class="control-label">秒杀开始时间</label>
        <div >
            <label style="color: red" class="control-label"  id="promoStartDate"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">商品名</label>
        <div >
            <label class="control-label"  id="title">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">商品描述</label>
        <div >
            <label class="control-label"  id="description">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">价格</label>
        <div >
            <label class="control-label"  id="price"/>
        </div>
    </div>
    <div id="seckillprice" class="form-group">
        <label style="color:red" class="control-label">秒杀价格</label>
        <div >
            <label style="color: red" class="control-label"  id="promoPrice"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">图片地址</label>
        <div >
            <img style="width: 200px;height: auto" id="imgUrl"/>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label">库存</label>
        <div >
            <label class="control-label"  id="stock"/>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label">销量</label>
        <div >
            <label class="control-label"  id="sales"/>
        </div>
    </div>
    <div class="form-actions">
        <button class="btn blue" id="createorder" type="submit">下单</button>
    </div>
</div>
<script>

    function getParam(paramName) {
        paramValue = "", isFound = !1;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
        }
        return paramValue == "" && (paramValue = null), paramValue
    }

    var item={}
    var promoId={}
    jQuery(document).ready(function () {


        $.ajax({
            type:"GET",

            url:"http://localhost:8080/item/getItem",
            data:{
                "id":getParam("id"),
            },
            xhrFields:{withCredentials: true},
            success:function (data) {
                if (data.status == "success") {
                    item=data.data
                    reloadDom();
                    setInterval(reloadDom,1000)
                }else{
                    alert("获取信息失败，原因是"+data.data.errMsg)
                }
            },
            error:function (data) {  //网络中断时被调用
                alert("获取信息失败，原因是与服务器网络中断或者"+data.responseText)
            }
        });


        $("#createorder").on("click",function () {
          //  console.log("createorder promo id: "+item.promo.id)
            $.ajax({
                type:"POST",

                url:"http://localhost:8080/order/createorder",
                contentType:"application/x-www-form-urlencoded",
                data:{
                    "itemId":item.id,
                    "amount":1,
                    "promoId":promoId


                },
                xhrFields:{withCredentials: true},
                success:function (data) {
                    if (data.status == "success") {
                        alert(data.data)
                        window.location.reload();
                    }else{
                        alert("下单失败，原因是"+data.data.errMsg)

                        if (data.data.errCode == 20003) {
                            window.location.href="login.html"
                        }
                    }
                },
                error:function (data) {  //网络中断时被调用
                    alert("下单失败，原因是与服务器网络中断或者"+data.responseText)
                }
            });
        })

    })

    function reloadDom() {
        $("#title").text(item.title);
        $("#description").text(item.description);
        $("#price").text(item.price);
        $("#imgUrl").attr("src",item.imgUrl);
        $("#stock").text(item.stock);
        $("#sales").text(item.sales);
        var promo=item.promo;
        if (promo!=null) {
            var promoStatus =promo.status
            var startTime=promo.startDate.replace(new RegExp("-","gm"),"/")
            startTime = new Date(startTime);
            var nowTime = Date.parse(new Date());
            var delta=(startTime-nowTime)/1000;


            //还未开始
            if(promoStatus==1){
                $("#promoStartDate").text("秒杀活动将于"+promo.startDate+" 开始售卖,倒计时："+delta+"秒！");
                $("#promoPrice").text(promo.promoItemPrice);
                $("#createorder").attr("disabled",true);
                if (delta <= 0) {//活动开始了
                    promoStatus=2
                    reloadDom();
                }
            }
            if(promoStatus==2){
                $("#promoStartDate").text("秒杀活动正在进行！")
                $("#promoPrice").text(promo.promoItemPrice)
                $("#createorder").attr("disabled",false)
            }
            promoId=promo.id;
        }else {
            $("#seckillmsg").hide()
            $("#seckillprice").hide()
        }

    }

</script>
</body>
</html>